<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialog"
    title="批量新增"
    width="600px"
    :close-on-click-modal="false"
    append-to-body
    @close="cancel"
  >
    <div>
      <el-form ref="suspendForm" :model="param" :rules="rules" size="small" label-width="120px">
        <el-form-item label="暂停原因：" prop="suspendReason">
          <el-input v-model="param.suspendReason" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="暂停日期区间：" prop="suspendDate">
          <el-date-picker
            v-model="suspendDate"
            value-format="yyyy-MM-dd"
            type="daterange"
            :clearable="false"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 400px;"
            @change="suspendDateChange"
          />
        </el-form-item>
        <el-form-item label="暂停时间区间：" prop="suspendTime">
          <el-time-picker
            v-model="suspendTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            is-range
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            style="width: 400px;"
            @change="suspendTimeChange"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { batchAdd } from '@/api/suspend'
import { parseTime } from '@/utils/index'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    var timeVali = (rule, value, callback) => {
      if (!this.param.startTime || !this.param.overTime) {
        callback(new Error('请选择暂停时间段'))
      } else {
        callback()
      }
    }
    var dateVali = (rule, value, callback) => {
      if (!this.param.startDate || !this.param.overDate) {
        callback(new Error('请选择暂停日期段'))
      } else {
        callback()
      }
    }
    return {
      dialog: false,
      loading: false,
      param: {},
      suspendTime: [],
      suspendDate: [],
      rules: {
        suspendReason: [
          { required: true, message: '请填写暂停原因', trigger: 'blur' }
        ],
        suspendTime: [
          { validator: timeVali, trigger: 'change' }
        ],
        suspendDate: [
          { validator: dateVali, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.dialog = false
      this.param = {}
      this.suspendDate = []
      this.suspendTime = []
    },
    // 时间组件触发
    suspendTimeChange(val) {
      this.param.startTime = parseTime(val[0])
      this.param.overTime = parseTime(val[1])
    },
    // 日期组件触发
    suspendDateChange(val) {
      this.param.startDate = val[0]
      this.param.overDate = val[1]
    },
    // 确定按钮触发
    doSubmit() {
      this.$refs.suspendForm.validate((valid) => {
        if (valid) {
          this.loading = true
          batchAdd(this.param).then(res => {
            this.parent.toQuery()
            Message({
              message: '操作成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
            this.cancel()
          }, error => {
            console.log(error)
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
